Page({
  data: {
    historyWords: [],
    searchValue: '',
  },

  onShow() {
    this.queryHistory();
  },

  queryHistory() {
    let arr = wx.getStorageSync('historyWords')
    console.log(arr);
    this.setData({
      historyWords: arr || []
    })
    console.log(this.data.historyWords);
  },

  handleClearHistory() {
    wx.showModal({
      content: '是否清空历史记录？',
      complete: (res) => {
        if (res.confirm) {
          wx.removeStorage({
            key: 'historyWords',
            success: res=>{
              console.log(res)
              this.setData({
                historyWords: []
              })
              wx.showToast({
                title: '清除成功',
                duration: 800
              })
            }
          })
        }
      }
    })
  },

  handleHistoryTap(e) {
    const { historyWords } = this.data;
    const { dataset } = e.currentTarget;
    const _searchValue = historyWords[dataset.index || 0] || '';
    if (_searchValue) {
      wx.navigateTo({
        url: `/pages/goods/result/index?searchValue=${_searchValue}`,
      });
    }
  },

  handleSubmit(e) {
    const { value } = e.detail;
    if ((value.trim()).length === 0) return;
    wx.navigateTo({
      url: `/pages/goods/result/index?searchValue=${value}`,
    });
  },
});
